package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.QueryProductListRequest;
import com.aliyun.iot20180120.models.QueryProductListResponse;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.framework.config.properties.AliIoTConfigProperties;
import com.zzyl.nursing.controller.DeviceController;
import com.zzyl.nursing.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 设备Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-10
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService
{
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;
    @Autowired
    private Client client;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public static final String IOT_PRODUCT_LIST_ALL= "iot:all_product";

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Device selectDeviceById(Long id)
    {
        return deviceMapper.selectById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param device 设备
     * @return 设备
     */
    @Override
    public List<Device> selectDeviceList(Device device)
    {
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 新增设备
     * 
     * @param device 设备
     * @return 结果
     */
    @Override
    public int insertDevice(Device device)
    {
        return deviceMapper.insert(device);
    }

    /**
     * 修改设备
     * 
     * @param device 设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        return deviceMapper.updateById(device);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(Long[] ids)
    {
        return deviceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceById(Long id)
    {
        return deviceMapper.deleteById(id);
    }

    @Override
    public void syncProductList() {
        // 请求参数
        QueryProductListRequest request = new QueryProductListRequest();
        // 当前页码
        request.setCurrentPage(1);
        // 每页显示条数
        request.setPageSize(100);
        // 实例ID
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        // 调用第三方查询产品列表  请求返回的响应对象
        QueryProductListResponse response;
        try {
            response = client.queryProductList(request);
            System.out.println(response);
        } catch (Exception e) {
            throw new BaseException("阿里云IoT查询产品列表失败");
        }

        // 判断，如果数据为空，则中断请求
        if (!response.getBody().success) {
            throw new BaseException("阿里云IoT查询产品列表失败");
        }

        // 存储到redis                                                               请求返回的响应对象.获取响应体中的数据.获取响应体中的实际数据部分.获取列表数据.获取产品信息列表
        redisTemplate.opsForValue().set(IOT_PRODUCT_LIST_ALL, JSONUtil.toJsonPrettyStr(response.getBody().getData().getList().getProductInfo()));
    }



        /**
         * 从Redis中获取产品列表
         *
         * @return 产品列表
         */
        @Override
        public List<ProductVo> getProductList() {
            // 1.查询Redis获取所有产品列表
            String productListStr = redisTemplate.opsForValue().get(IOT_PRODUCT_LIST_ALL);

            // 2.将查询到的结果响应给前端
            return JSONUtil.toList(productListStr, ProductVo.class);
        }
}

